package com.y.game.view;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import com.y.game.emtity.Ground;
import com.y.game.emtity.Shape;
import com.y.game.util.Util;

/**
 * 游戏面板 用户显示 障碍物 和 图形
 */
public class GamePanel extends JPanel {
	private Ground ground;
	private Shape shape;
	
	// 构造函数
	public GamePanel() {
		this.setSize(Util.CELL_SIZE * Util.GAMEPANEL_SIZE[0], 
				Util.CELL_SIZE * Util.GAMEPANEL_SIZE[1]);
	}
	
	public void display(Ground ground, Shape shape) {
//System.out.println("GamePanel display");
		
		this.ground = ground;
		this.shape = shape;
		
		this.repaint();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//System.out.println("-- paintComponent");
		
		// 画之前清除原来图形印记
		Color c = g.getColor();
		g.setColor(Util.GAMEPANEL_COLOR);
		g.fillRect(0, 0, 
				Util.CELL_SIZE * Util.GAMEPANEL_SIZE[0], 
				Util.CELL_SIZE * Util.GAMEPANEL_SIZE[1]);
		g.setColor(c);
		
		if(null != ground && null != shape) {
			this.ground.drawMe(g);
			this.shape.drawMe(g);
		}
	}
}
